package com.cai310.lottery.ticket.protocol.local.utils;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;

import com.cai310.lottery.Constant;
import com.cai310.lottery.PlConstant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.entity.lottery.ticket.Ticket;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.support.ContentBean;
import com.cai310.lottery.support.pl.PlCompoundContent;
import com.cai310.lottery.support.pl.PlContentBeanBuilder;
import com.cai310.lottery.support.pl.PlPlayType;
import com.cai310.lottery.support.pl.PlPlayType;

public  class PLCPUtil extends CPUtil{
	/**
	 *  复试已经处理了。单式还没处理
	 * @param lottery
	 * @param playType
	 * @return
	 */
	private static final NumberFormat ONE_NF = new DecimalFormat("0");
	private static final String NUMCODE = ",";
	private static final String CONTENTCONNECTCODE = "-";
	private static final String DANSPLITCODE = ":";
	private static final String SINGSPILTCODE = "+";
	@Override
	public String getCpLotteryId(Lottery lottery,Byte betType){
		return "7";
	}
	@Override
	public String getCpBetType(Ticket ticket){
		Byte betType = ticket.getBetType();
		return ""+betType;
	}
	@Override
	public String getCpPlayType(Ticket ticket){
		return ticket.getExtension();
	}
	@Override
	public String getBetContent(Ticket ticket){
		return ticket.getContent();
	}
	@Override
	public String getSpecialFlag(Ticket ticket) {
		return "";
	}
	@Override
	public String getUpdatePeriodNumber(Ticket ticket) {
		return null;
	}
	public static SalesMode getSalesMode(Ticket ticket) throws DataException{
		String extension = ticket.getExtension();
		if("1".equals(extension)||"6".equals(extension)){
			  return SalesMode.SINGLE;
		}else if("0".equals(extension)||"2".equals(extension)||"5".equals(extension)){
            return SalesMode.COMPOUND;
		}else{
			//报错
			throw new DataException("组合出票投注玩法出错");
		}
	}
	protected static String buildSingleStr(String content){
		content = content.replaceAll("\\"+SINGSPILTCODE, "\r\n");
		String[] arr = content.split("(\r\n|\n)+");
		StringBuffer sb = new StringBuffer();
		StringBuffer bet;
		for (int i = 0; i < arr.length; i++) {
			String betStr = arr[i];
			bet= new StringBuffer();
			for (int j = 0; j < betStr.length(); j++) {
				char num = betStr.charAt(j);
				if(j==betStr.length()-1){
					bet.append(num);
				}else{
					bet.append(num).append(Constant.SINGLE_SEPARATOR_FOR_NUMBER);
				}
			}
			if(i==arr.length-1){
				sb.append(bet.toString());
			}else{
				sb.append(bet.toString()).append("\r\n");
			}
			
			
		}
		return sb.toString();
		
		
	}
	public static PlPlayType getPlayType(Ticket ticket) throws DataException{
		Byte betType = ticket.getBetType();
		PlPlayType plPlayType = null;
		String extension = ticket.getExtension();
		if(betType.equals(Byte.valueOf("0"))){
			if("1".equals(extension)||"5".equals(extension)){
			     return PlPlayType.P3Direct;
			}else if("6".equals(extension)){
				///排列三组三组六单式判断
				String[] arr = PLCPUtil.buildSingleStr(ticket.getContent()).split("(\r\n|\n)+");
				String[] betArr = arr[0].split(Constant.SINGLE_SEPARATOR_FOR_NUMBER);
				if(betArr[0].equals(betArr[1])||betArr[1].equals(betArr[2])||betArr[0].equals(betArr[2])){
					//三个之中有两个相同
					 return PlPlayType.Group3;
				}else{
					return PlPlayType.Group6;
				}
			}
	    }else if(betType.equals(Byte.valueOf("1"))){
	    	return PlPlayType.P5Direct;
	    }else if(betType.equals(Byte.valueOf("2"))){
	    	return PlPlayType.Group3;
	    }else if(betType.equals(Byte.valueOf("3"))){
	    	return PlPlayType.Group6;
	    }else if(betType.equals(Byte.valueOf("4"))){
	    	return PlPlayType.DirectSum;
	    }else if(betType.equals(Byte.valueOf("5"))){
	    	return PlPlayType.GroupSum;
	    }else if(betType.equals(Byte.valueOf("6"))){
	    	return PlPlayType.BaoChuan;
	    }else if(betType.equals(Byte.valueOf("10"))){
	    	return PlPlayType.P3DirectKuadu;
	    }else if(betType.equals(Byte.valueOf("11"))){
	    	return PlPlayType.P3Group3Kuadu;
	    }else if(betType.equals(Byte.valueOf("12"))){
	    	return PlPlayType.P3Group6Kuadu;
	    }else{
			//报错
			throw new DataException("组合出票玩法出错");
		}
		return plPlayType;
	}
	public static ContentBean buildContentBean(Ticket ticket) throws DataException{
		Byte betType = ticket.getBetType();
		PlPlayType plPlayType = PLCPUtil.getPlayType(ticket);

		SalesMode salesMode = PLCPUtil.getSalesMode(ticket);
		if(SalesMode.SINGLE.equals(salesMode)){
			return PlContentBeanBuilder.buildSingleContentBean(buildSingleStr(ticket.getContent()), plPlayType);
		}else if(SalesMode.COMPOUND.equals(salesMode)){
				if(ticket.getContent().indexOf(NUMCODE)!=-1){
					return PlContentBeanBuilder.buildCompoundContentBean(buildCompoundStr(ticket.getContent(),ticket.getUnits()), plPlayType);
				}else{
					//报错
					throw new DataException("组合出票内容出错");
				}
		}else{
			//报错
			throw new DataException("组合出票投注玩法出错");
		}
	}
	protected static String buildCompoundStr(String content,Integer units){
		StringBuffer sb = new StringBuffer();
		sb.append(units+":"+content);
		return sb.toString();
	}	
	public static Collection<PlCompoundContent> getCompoundContent(Ticket ticket) throws DataException {
		Collection<PlCompoundContent> c = JSONArray.toCollection(JSONArray.fromObject(buildContentBean(ticket).getContent()),
				PlCompoundContent.class);
		return c;
	}
}
